package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealdishMapper;
    @Autowired
    DishMapper dishMapper;
    /**
     * 套餐分页
     * @param setmealPageQueryDTO
     * @return
     * */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<SetmealVO>page= setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增菜品
     * @param setmealDTO
     * * @return
     * */
    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表插入一条数据
        setmealMapper .insert(setmeal);
        //获取套餐主键ID
        Long setmealId = setmeal.getId();

        List<SetmealDish> dishs=setmealDTO.getSetmealDishes();
        if (dishs!=null&&dishs.size()>0){
            dishs.forEach(dish->{
                dish.setSetmealId(setmealId);
                //批量插入
                setmealdishMapper.insert(dish);
            });

        }
    }

    /** 启用禁用套餐
     *  @param status
     * * @param id
     */
    @Override
    @Transactional
    public void startOrstop(Integer status, long id) {
        //创建setmeal对象
        Setmeal setmeal=Setmeal.builder()
                .id(id)
                .status(status)
                .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .build();

        //查询关联套餐里面的菜品
        List<SetmealDish>dishes=setmealdishMapper.getSetmealId(id);
        if(status==StatusConstant.ENABLE) {
            dishes.forEach(dish -> {
                //获取菜品对象
                Dish dishDemo = dishMapper.getById(dish.getDishId());
                //查看菜品里面的状态，1 or 0
                if (dishDemo.getStatus() != StatusConstant.ENABLE) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        setmealMapper.update(setmeal);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDTO setmealDTO) {
        //创建setmeal对象，并把setmeal里面的值传输过去
        Setmeal setmeal=Setmeal.builder()
                .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .build();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改setmeal表，进行update操作
        setmealMapper.update(setmeal);

        //删除指定套餐关联食物的数据
     setmealdishMapper.deleteBySetmealID(setmealDTO.getId());

     //插入最新的DISH数据，操作setmeal_dish表，更新数据
        List<SetmealDish> dishes=setmealDTO.getSetmealDishes();
        if (dishes!=null&&dishes.size()>0){
            dishes.forEach(dish->{
                dish.setSetmealId(setmealDTO.getId());
                //批量插入
                setmealdishMapper.insert(dish);
            });

        }
    }

    /**
     * 根据分类id查询套餐与关联的菜品
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealVO getId(Long id) {
        //根据id创建对应的setmeal对象
        Setmeal setmeal=setmealMapper.getById(id);

        //查询关联套餐里面的菜品
        List<SetmealDish>dishes=setmealdishMapper.getSetmealId(id);

        //封装成VO
        SetmealVO setmealVO=SetmealVO.builder().setmealDishes(dishes).build();
        BeanUtils.copyProperties(setmeal,setmealVO);

        return setmealVO;
    }

    /**
     * 批量删除套餐
     *  @param ids
     * @return
     * */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
    for(Long id:ids){
    Setmeal setmeal=setmealMapper.getById(id);
    if (setmeal.getStatus()== StatusConstant.ENABLE){
        //当前菜品处于起售当中
        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
    }
        }
    //删除套餐关联菜品的数据
        setmealdishMapper.delete(ids);
    //删除套餐
        setmealMapper.delete(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
